package com.abiao.shop.result;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

/**
 * @Description: Result 封装向用户的返回结果
 * @Author: AB
 * @Date: 2021-07-04
 * @Version:v1.0
 */

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Result<T> {
    private int code;
    private String msg;
    private T data;

    private Result(CodeMsg msg){
        if (msg == null){
            return;
        }
        this.code = msg.getCode();
        this.msg = msg.getMsg();
    }

    private Result(T data) {
        this.code = 0;
        this.msg = "success";
        this.data = data;
    }

    /**
     * 成功时候调用
     */
    public static <T> Result<T> success(T data){
        return new Result<T>(data);
    }

    /**
     * 失败时候调用
     */
    public static <T> Result<T> error(CodeMsg msg){
        return new Result<T>(msg);
    }
}
